package com.baosight.risk.common.quartz;

import com.baosight.risk.api.riim.RiimRiskImportService;
import com.baosight.risk.common.utils.DateUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.quartz.SysJobParamMapper;
import com.baosight.risk.mapper.xtzy.XtzyCompanyMapper;
import com.baosight.risk.service.quartz.entity.SysJobParam;
import com.baosight.risk.service.xtzy.entity.XtzyCompany;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * @author huangxin
 * <p>
 * 定时任务
 * 重点风险披露报告(基地)
 */
@Slf4j
@Component("rireRiskReportTaskSub")
public class RireRiskReportTaskSub {
    @Autowired
    private RiimRiskImportService importService;

    @Autowired
    private XtzyCompanyMapper companyMapper;

    @Autowired
    private SysJobParamMapper sysJobParamMapper;


    public void sendInformation(String params, String jobId) throws Exception {
        System.out.println("进入定时任务————重点风险披露报告(基地)");
        String acctYear = "";
        String quarterCurrent = "";
        String manageCompCode = "";
        String manageCompName = "";
        String timingTaskId="";
        if (StringUtils.isNotEmpty(params) && params.indexOf(",") > 0) {
            String[] split = params.split(",");
            acctYear = split[0];
            quarterCurrent = split[1];
            manageCompCode = split[2];
            manageCompName = split[3];
            timingTaskId=split[4];
        }

        List<String> manageCompCodeList = new ArrayList<String>();
        List<String> manageCompNameList = new ArrayList<String>();
        List<XtzyCompany> companyList=new ArrayList<XtzyCompany>();
        if(StringUtils.isNotEmpty(manageCompCode)){
            String [] manageCompCodeData=manageCompCode.split("\\\\");
            String [] manageCompNameData=manageCompName.split("\\\\");
            manageCompCodeList= Arrays.asList(manageCompCodeData);
            manageCompNameList= Arrays.asList(manageCompNameData);
        }
        if(StringUtils.isEmpty(manageCompCode)){
            SysJobParam param=new SysJobParam();
            param.setJobGuid(timingTaskId);
            List<SysJobParam> jobParamList= sysJobParamMapper.select(param);
            if (StringUtils.isEmpty(manageCompCode) && jobParamList.size()>0) {
                String [] manageCompCodeData=manageCompCode.split("\\\\");
                manageCompCodeList= Arrays.asList(manageCompCodeData);;
            }
            if (StringUtils.isEmpty(manageCompName)) {
                String [] manageCompNameData=manageCompName.split("\\\\");
                manageCompNameList= Arrays.asList(manageCompNameData);
            }
        }
        if (StringUtils.isEmpty(acctYear)) {
            acctYear = DateUtils.format(new Date(), "yyyy");
        }
        if (StringUtils.isEmpty(quarterCurrent)) {
            quarterCurrent = DateUtils.getQuarter(new Date());
        }
        for (int i=0;i<manageCompCodeList.size();i++){
            importService.saveSendReport(params,manageCompCodeList.get(i),jobId);
        }
        System.out.println("执行结束————重点风险披露报告(基地)");
    }

    public void testNoParams() {
        log.error("执行无参方法");
    }
}